/**********************************************
 * 	file.java								  *
 *  Course: CS3343							  *
 *  Version: 2.5     Release 1				  *
 *  Last Modified : 22 MAR 2013			      *
 **********************************************/
package file;

import java.io.*;
import java.net.URL;

public class FileIO {

	public FileIO() {
		
	}

	// get all the texts from the file
	public String getFileContents(String fileName) {
		String sentence = "";
		try {
		
	
			String path = System.getProperty("user.dir") +"/input/" + fileName ;
			File file = new File (path);
			BufferedReader buf = new BufferedReader(new FileReader(file));
			String line;
			while ((line = buf.readLine()) != null) {
				sentence = sentence + line.trim() + " ";
			}
			buf.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return sentence.trim();
		
	}

	//this function determines which text file to be accessed 
	public String getResolvedFileName(String word, String type){
		
		 int length = word.length()>15 ? 15: word.length();
		 
		 String resolvedFileName= "data/mumber" ;
        if(!Character.isLetter(word.charAt(0)))
            resolvedFileName="data/special";
        else if (type.equalsIgnoreCase("dictionary"))
			resolvedFileName= "data/dictionary/" + word.toLowerCase().charAt(0) + "/" + length ;
		else if (type.equalsIgnoreCase("noun"))
			resolvedFileName= "data/noun/" + word.toLowerCase().charAt(0)+ "/" + length ;
		else if (type.equalsIgnoreCase("adverb"))
			resolvedFileName= "data/adverb/" + word.toLowerCase().charAt(0)+ "/" + length;
		else if (type.equalsIgnoreCase("adjective"))
			resolvedFileName= "data/adjective/" + word.toLowerCase().charAt(0)+ "/" + length;
		else if (type.equalsIgnoreCase("verb"))
			resolvedFileName= "data/verb/" + word.toLowerCase().charAt(0)+ "/" + length;
		else if (type.equalsIgnoreCase("preposition"))
			resolvedFileName= "data/preposition/preposition";
		return resolvedFileName;
	
	}
	
	//get all the related words in a specific category, such as adjectives starting with letter "s" 
	public String[] getWordCategory(String word, String type) {
		String[] wordList = null;
		try {
			String line;
			String contents = "";

			String path = System.getProperty("user.dir") +"/bin/file/" + getResolvedFileName(word, type)+  ".txt";
			File file = new File (path);
			BufferedReader buf = new BufferedReader(new FileReader(file));
			while ((line = buf.readLine()) != null) {
					contents = contents + line.trim() + " ";
			}

			wordList = contents.split(" ");
			buf.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return wordList;
	}
	
	public boolean writeToFile(String article, String fileName){
		try{
			String path = System.getProperty("user.dir") +"/output/" + fileName ;
			File file = new File (path);
			file.delete();
            file.createNewFile();

			FileWriter outFile = new FileWriter(file);
	        PrintWriter out = new PrintWriter(outFile);
	        out.print(article);
	        out.close();
	        System.out.println("Output has been written to " + fileName);
	        System.out.println();
	        return true;
	        
	        
		}catch (Exception ex){
			System.out.println(ex.getMessage());
			ex.printStackTrace();
			return false;
		}

		

	}
	
	
}
